using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using MacomberMap.Common.User_Interfaces.Components;
using System.ComponentModel;
using System.Windows.Forms;
using System.Drawing;
using MacomberMap.Common.User_Interfaces.One_Lines.Elements;
using MacomberMap.Common.Components;
using MacomberMap.Common.Types;
using MacomberMap.Common.Database_Interface;

namespace MacomberMap.Common.User_Interfaces.One_Lines.Elements
{
    /// <summary>
    /// This class provides a viewable LAAR
    /// </summary>
    public class MM_OneLine_LAAR : MM_OneLine_Element
    {
        #region Initialization
        /// <summary>
        /// Initialize a new viewable LAAR
        /// </summary>
        /// <param name="ElemConfig">The Xml configuration for the LAAR</param>
        /// <param name="DisplayParameters">The display parameters for the LAAR</param>
        /// <param name="DbConnection">The database connection</param>
        public MM_OneLine_LAAR(XmlElement ElemConfig, MM_OneLine_Display DisplayParameters, MM_Database DbConnection)
            : base(ElemConfig, DisplayParameters, DbConnection)
        {
            //Retrieve our arrow direction from the LAAR
            Orientation = ParseOrientation(ElemConfig.Attributes["Orientation"].Value);            
        }

        /// <summary>
        /// Initialize a new viewable LAAR
        /// </summary>
        /// <param name="SelectedElement">The CIM element upon which the LAAR is based</param>
        /// <param name="DisplayParameters">The display parameters</param>
        /// <param name="Orientation">The orientation of the LaaR</param>
        public MM_OneLine_LAAR(MM_Element SelectedElement, MM_OneLine_Display DisplayParameters, MM_OneLine_Element.enumOrientations Orientation)
            : base(SelectedElement, DisplayParameters, Orientation)
        {                 
        }
        #endregion


        #region Rendering
        /// <summary>
        /// Draw the image
        /// </summary>
        /// <param name="g"></param>
        public override void DrawImage(Graphics g)
        {
            Point CenterBounds = CenterRect(DisplayRectangle);
            Pen ThisPen = KVLevel != null ? KVLevel.Energized.ForePen : Pens.Red;
            if (Orientation == enumOrientations.Up)
            {
                g.DrawLine(ThisPen, CenterBounds.X, CenterBounds.Y, CenterBounds.X, Height - 1);
                g.DrawPolygon(ThisPen, IntToPoints(0, CenterBounds.Y, Width - 1, CenterBounds.Y, CenterBounds.X, 0, 0, CenterBounds.Y));
                g.DrawLine(ThisPen, 0, 0, Width - 1, Height - 1);
            }
            else if (Orientation == enumOrientations.Down)
            {
                g.DrawLine(ThisPen, CenterBounds.X, 0, CenterBounds.X, CenterBounds.Y);
                g.DrawPolygon(ThisPen, IntToPoints(0, CenterBounds.Y, Width - 1, CenterBounds.Y, CenterBounds.X, Height - 1, 0, CenterBounds.Y));
                g.DrawLine(ThisPen, 0, 0, Width - 1, Height - 1);
            }
            else if (Orientation == enumOrientations.Left)
            {
                g.DrawLine(ThisPen, CenterBounds.X, CenterBounds.Y, Width - 1, CenterBounds.Y);
                g.DrawPolygon(ThisPen, IntToPoints(CenterBounds.X, 0, CenterBounds.X, Height - 1, 0, CenterBounds.Y, CenterBounds.X, 0));
                g.DrawLine(ThisPen, 0, 0, Width - 1, Height - 1);
            }
            else if (Orientation == enumOrientations.Right)
            {
                g.DrawLine(ThisPen, 0, CenterBounds.Y, CenterBounds.X, CenterBounds.Y);
                g.DrawPolygon(ThisPen, IntToPoints(CenterBounds.X, 0, CenterBounds.X, Height - 1, Width - 1, CenterBounds.Y, CenterBounds.X, 0));
                g.DrawLine(ThisPen, 0, 0, Width - 1, Height - 1);
            }
            else if (Orientation == enumOrientations.Horizontal || Orientation == enumOrientations.Unknown)
            {
                g.DrawPolygon(ThisPen, IntToPoints(0, 0, 0, Height - 1, CenterBounds.X, CenterBounds.Y, 0, 0));
                g.DrawPolygon(ThisPen, IntToPoints(Width - 1, 0, Width - 1, Height - 1, CenterBounds.X, CenterBounds.Y, Width - 1, 0));
                g.DrawLine(ThisPen, CenterBounds.X / 2, 0, CenterBounds.X / 2, Height - 1);
                g.DrawLine(ThisPen, (CenterBounds.X / 2) + CenterBounds.X, 0, (CenterBounds.X / 2) + CenterBounds.X, Height - 1);
            }
            else if (Orientation == enumOrientations.Vertical)
            {
                g.DrawPolygon(ThisPen, IntToPoints(0, 0, Width - 1, 0, CenterBounds.X, CenterBounds.Y, 0, 0));
                g.DrawPolygon(ThisPen, IntToPoints(0, Height - 1, Width - 1, Height - 1, CenterBounds.X, CenterBounds.Y, 0, Height - 1));
                g.DrawLine(ThisPen, 0, CenterBounds.Y / 2, Width - 1, CenterBounds.Y / 2);
                g.DrawLine(ThisPen, 0, (CenterBounds.Y / 2) + CenterBounds.Y, Width - 1, (CenterBounds.Y / 2) + CenterBounds.Y);
            }
        }



        /// <summary>
        /// Draw the LAAR text
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TextLabel_Paint(object sender, PaintEventArgs e)
        {
            if (DisplayParameters.Names)
                e.Graphics.DrawString(this.Name, SystemFonts.CaptionFont, Brushes.Aqua, PointF.Empty);
        }
        #endregion
    }
}
